//
//  PioneerSpringAnimation.m
//  BezierPathDemo
//
//  Created by mac on 16/3/14.
//  Copyright © 2016年 Pioneer. All rights reserved.
//

#import "PioneerSpringAnimation.h"
#define MainFrame [UIScreen mainScreen].bounds
#define MainWidth CGRectGetWidth(MainFrame)
#define MainHeight CGRectGetHeight(MainFrame)
#define CurveAnimaitonHeight 120
#define RandomCGColor [[UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255. blue:arc4random()%256/255. alpha:1] CGColor]
@interface PioneerSpringAnimation ()
@property (nonatomic, strong) CAShapeLayer *shapeLayer;
@property (nonatomic, assign) CGPoint firstPoint,secondPoint,thirdPoint,lastPoint;
@property (nonatomic, assign) CGFloat curveX,curveY;
@property (nonatomic, assign) PioneerType type;
@property (nonatomic, assign) CGFloat orginHeight;
@property (nonatomic, strong) CADisplayLink *displayLink;
@property (nonatomic, strong) UIView *controlView;
@property (nonatomic, assign) BOOL isAnimation;
@property (nonatomic, assign) BOOL isFirst;

@end

@implementation PioneerSpringAnimation
- (instancetype)initWithType:(PioneerType)type andOriginHeight:(float)height andControlView:(UIView *)controlView;
{
    if (self = [super init]) {
        
        self.orginHeight = height;
        self.type = type;
        self.controlView = controlView;
        [self.controlView.layer addSublayer:self.shapeLayer];
        
        [self configAction];
        [self displayLink];
        [self calculatePath];
        
    }
    return self;
}

- (void)setSpringBackground:(UIColor *)color
{
    _shapeLayer.fillColor = color.CGColor;
}
- (CAShapeLayer *)shapeLayer
{
    if(!_shapeLayer)
    {
        _shapeLayer = [[CAShapeLayer alloc]init];
        _shapeLayer.fillColor =  RandomCGColor;
    }
    
    return _shapeLayer;
}
- (void)setOrginHeight:(CGFloat)orginHeight
{
    if (orginHeight <= 0) {
        orginHeight = 0;
    }
    _orginHeight = orginHeight;
}
- (void)setType:(PioneerType)type
{
    _type = type;
    _isFirst = NO;
    
    switch (type) {
        case PioneerTypeTop:
        {
            _firstPoint = CGPointZero;
            _secondPoint = CGPointMake(MainWidth, 0);
            _thirdPoint = CGPointMake(MainWidth, _orginHeight);
            _lastPoint = CGPointMake(0, _orginHeight);
        }
            break;
        case PioneerTypeLeft:
        {
            _firstPoint = CGPointZero;
            _secondPoint = CGPointMake(0, MainHeight);
            _thirdPoint = CGPointMake(_orginHeight, MainHeight);
            _lastPoint = CGPointMake(_orginHeight, 0);
        }
            break;
        case PioneerTypeBottom:
        {
            _firstPoint = CGPointMake(MainWidth, MainHeight);
            _secondPoint = CGPointMake(0, MainHeight);
            _thirdPoint = CGPointMake(0, MainHeight - _orginHeight);
            _lastPoint = CGPointMake(MainWidth, MainHeight - _orginHeight);
        }
            break;
        case PioneerTypeRight:
        {
            _firstPoint = CGPointMake(MainWidth, MainHeight);
            _secondPoint = CGPointMake(MainWidth, 0);
            _thirdPoint = CGPointMake(MainWidth - _orginHeight, 0);
            _lastPoint = CGPointMake(MainWidth - _orginHeight, MainHeight);
        }
            break;
            
        default:
            break;
    }
    [self calculatePath];
    
}
- (void)configAction
{
    
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePanAction:)];
    [_controlView addGestureRecognizer:pan];
    
}
- (void)handlePanAction:(UIPanGestureRecognizer *)pan
{
    if (!_isAnimation) {
        if (pan.state == UIGestureRecognizerStateChanged) {
            CGPoint point = [pan translationInView:_controlView];
            float height;
            switch (_type) {
                case PioneerTypeTop:
                {
                    height = point.y * 0.7 + _orginHeight;
                    _curveY = height >= _orginHeight ? height : _orginHeight;
                    _curveX = point.x + MainWidth/2;
                }
                    break;
                case PioneerTypeLeft:
                {
                    height = point.x * 0.7 + _orginHeight;
                    _curveX = height >= _orginHeight ? height : _orginHeight;
                    _curveY = point.y + MainHeight/2;
                }
                    break;
                case PioneerTypeRight:
                {
                    height = point.x * 0.7 + MainWidth -  _orginHeight;
                    _curveX = height >= MainWidth - _orginHeight ? MainWidth - _orginHeight: height;
                    _curveY = point.y + MainHeight/2;
                }
                    break;
                case PioneerTypeBottom:
                {
                    height = point.y * 0.7 + MainHeight - _orginHeight;
                    _curveY = height >= MainHeight -  _orginHeight ? MainHeight -  _orginHeight : height;
                    _curveX = point.x + MainWidth/2;
                }
                    break;
                default:
                    break;
            }
            
            
            
            
            [self updateShapeLayerPath];
        }else if (pan.state == UIGestureRecognizerStateCancelled || pan.state == UIGestureRecognizerStateEnded || pan.state == UIGestureRecognizerStateFailed){
            _isAnimation = YES;
            _displayLink.paused = NO;
            //弹簧动效
            [UIView animateWithDuration:1.0 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                [self initSpringAnimaitonPoint];
                
                [self updateShapeLayerPath];
            } completion:^(BOOL finished) {
                _isAnimation = NO;
                _displayLink.paused = YES;
            }];
        }
    }
}
- (CADisplayLink *)displayLink
{
    if(!_displayLink)
    {
        _displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(calculatePath)];
        [_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
        _displayLink.paused = YES;
    }
    return _displayLink;
}
- (void)updateShapeLayerPath
{
    //关键在于路线的绘制
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:_firstPoint];
    [path addLineToPoint:_secondPoint];
    [path addLineToPoint:_thirdPoint];
    [path addQuadCurveToPoint:_lastPoint controlPoint:CGPointMake(_curveX, _curveY)];
    [path closePath];
    _shapeLayer.path = path.CGPath;
}
- (void)calculatePath
{

    if (!_isFirst) {
        _isFirst = YES;
        //第一个问题
        [self initSpringAnimaitonPoint];
        
    }
    [self updateShapeLayerPath];
}
- (void)initSpringAnimaitonPoint
{
    switch (_type) {
        case PioneerTypeTop:
        {
            _curveX = MainWidth/2;
            _curveY = _orginHeight;
        }
            break;
        case PioneerTypeLeft:
        {
            _curveX = _orginHeight;
            _curveY = MainHeight/2;
            
        }
            break;
        case PioneerTypeRight:
        {
            _curveX = MainWidth - _orginHeight;
            _curveY = MainHeight/2;
        }
            break;
        case PioneerTypeBottom:
        {
            _curveX = MainWidth/2;
            _curveY = MainHeight -  _orginHeight;
        }
            break;
        default:
            break;
    }

}
@end
